package com.g2.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.g2.admin.common.constant.StatusTypeEnums;
import com.g2.admin.model.po.UserAccountPo;
import com.g2.admin.model.qo.AccountQo;
import com.g2.admin.service.UserAccountService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("account")
@Api(tags = "Account")
public class AccountController extends BaseController {
    @Autowired
    private UserAccountService userAccountService;


    @PostMapping("/add")
    public void add(@NotNull @Validated UserAccountPo userAccount) {
        userAccountService.addUserAccount(userAccount);
    }

    @GetMapping("/info")
    public UserAccountPo info() {
        return getUserAccount();
    }

    @GetMapping("/user")
    public UserAccountPo user(long id) {
        return userAccountService.getUserAccount(id);
    }

    @GetMapping("/list")
    public IPage<UserAccountPo> users(AccountQo accountQo) {
        return userAccountService.getAccountsAndPage(accountQo);
    }

    @GetMapping("/update")
    public void update(UserAccountPo accountPo) {
        userAccountService.updateUserAccountById(accountPo);
    }

    @PostMapping("/disable/{id}")
    public void disable(@PathVariable("id") long id) {
        updateStatus(id, StatusTypeEnums.StatusEnums.DISABLE);
    }

    @PostMapping("/enable/{id}")
    public void enable(@PathVariable("id") long id) {
        updateStatus(id, StatusTypeEnums.StatusEnums.ENABLE);
    }

    public void updateStatus(Long userId, StatusTypeEnums.StatusEnums status) {
        userAccountService.updateUserAccountById(UserAccountPo.builder().id(userId).status(status).build());
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable("id") long id) {
        userAccountService.delUserAccount(id);
    }
}
